package lib.self.ex.activity.list;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ItemAnimator;
import androidx.recyclerview.widget.RecyclerView.ItemDecoration;
import androidx.recyclerview.widget.RecyclerView.LayoutManager;
import androidx.recyclerview.widget.RecyclerView.OnScrollListener;

import java.util.List;

import lib.self.R;
import lib.self.adapter.interfaces.OnAdapterClickListener;
import lib.self.adapter.recycler.MultiTreeRecyclerAdapterEx;
import lib.self.bean.TreeBean;
import lib.self.bean.TreeChildBean;
import lib.self.ex.activity.ActivityEx;
import lib.self.view.recycler.WrapRecyclerView;
import lib.self.widget.list.OnTreeRecyclerWidgetListener;
import lib.self.widget.list.TreeRecyclerWidget;

/**
 * description: 二级数组列表界面基类
 * author: Steven Xiao
 * date: 2018/8/21
 */
public abstract class TreeRecyclerActivityEx<T extends TreeBean<C>, C extends TreeChildBean> extends ActivityEx
        implements OnTreeRecyclerWidgetListener<T, C>, OnAdapterClickListener {

    private TreeRecyclerWidget<T, C> mWidget = new TreeRecyclerWidget<>(this);

    @Override
    public int getContentViewId() {
        return R.layout.recyclerview_match_parent;
    }

    @Override
    public void findViews() {
        mWidget.findViews(getDecorView(), getRvResId(), getHeaderView(), getFooterView(), getNoMoreDataView(), getEmptyView());
    }

    @Override
    public void setViewsValue() {
        if (mWidget.isAdapterNull()) {
            mWidget.initAdapter(initAdapter());
        }
        mWidget.setViewsValue(initLayoutManager(), initItemDecoration(), initItemAnimator());

        setOnAdapterClickListener(this);
    }

    /**
     * 设置divider样式
     *
     * @return
     */
    protected ItemDecoration initItemDecoration() {
        return null;
    }

    /**
     * 设置增加删除动画
     *
     * @return
     */
    protected ItemAnimator initItemAnimator() {
        return null;
    }

    /**
     * 设置默认的布局样式（方向等）
     *
     * @return
     */
    protected LayoutManager initLayoutManager() {
        return new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
    }

    @Override
    public int getRvResId() {
        return R.id.recycler_view;
    }

    @Override
    public View getHeaderView() {
        return null;
    }

    @Override
    public View getFooterView() {
        return null;
    }

    @Override
    public View getNoMoreDataView() {
        return null;
    }

    @Override
    public View getEmptyView() {
        return null;
    }

    @Override
    public abstract MultiTreeRecyclerAdapterEx initAdapter();

    @Override
    public MultiTreeRecyclerAdapterEx getAdapter() {
        return (MultiTreeRecyclerAdapterEx) mWidget.getAdapter();
    }

    @Override
    public void onParentItemClick(View view, int position, int parentPosition) {
    }

    @Override
    public void onParentItemLongClick(View view, int position, int parentPosition) {
    }

    @Override
    public void onChildItemClick(View view, int position, int parentPosition, int childPosition) {
    }

    @Override
    public void onChildItemLongClick(View view, int position, int parentPosition, int childPosition) {
    }

    @Override
    public void hideFooterView() {
        mWidget.hideFooterView();
    }

    @Override
    public void showFooterView() {
        mWidget.showFooterView();
    }

    @Override
    public void showHeaderView() {
        mWidget.showHeaderView();
    }

    @Override
    public void hideHeaderView() {
        mWidget.hideHeaderView();
    }

    @Override
    public void showNoMoreDataView() {
        mWidget.showNoMoreDataView();
    }

    @Override
    public void hideNoMoreDataView() {
        mWidget.hideNoMoreDataView();
    }

    @Override
    public void showEmptyView() {
        mWidget.showEmptyView();
    }

    @Override
    public void hideEmptyView() {
        mWidget.hideEmptyView();
    }

    @Override
    public boolean enableLongClick() {
        return false;
    }

    @Override
    public void addItem(T item) {
        mWidget.addItem(item);
    }

    @Override
    public void addItem(int position, T item) {
        mWidget.addItem(position, item);
    }

    @Override
    public void addAll(List<T> data) {
        mWidget.addAll(data);
    }

    @Override
    public void addAll(int position, List<T> list) {
        mWidget.addAll(position, list);
    }

    @Override
    public void invalidate() {
        mWidget.invalidate();
    }

    @Override
    public void notifyItemChanged(int position) {
        mWidget.notifyItemChanged(position);
    }

    public void remove(int position) {
        mWidget.remove(position);
    }

    @Override
    public void remove(T item) {
        mWidget.remove(item);
    }

    @Override
    public void removeAll() {
        mWidget.removeAll();
    }

    @Override
    public List<T> getData() {
        return mWidget.getData();
    }

    @Override
    public void setData(List list) {
        mWidget.setData(list);
    }

    @Override
    public int getCount() {
        return mWidget.getCount();
    }

    @Override
    public int getLastItemPosition() {
        return mWidget.getLastItemPosition();
    }

    @Override
    public T getItem(int position) {
        return mWidget.getItem(position);
    }

    @Override
    public C getChildItem(int parentPosition, int childPosition) {
        return mWidget.getChildItem(parentPosition, childPosition);
    }

    @Override
    public T getRealItem(int position) {
        return mWidget.getRealItem(position);
    }

    @Override
    public T getParentItem(int position) {
        return mWidget.getParentItem(position);
    }

    @Override
    public boolean isEmpty() {
        return mWidget.isEmpty();
    }

    @Override
    public void setOnAdapterClickListener(OnAdapterClickListener listener) {
        mWidget.setOnAdapterClickListener(listener);
    }

    @Override
    public void setOnScrollListener(OnScrollListener listener) {
        mWidget.addOnScrollListener(listener);
    }

    @Override
    public int getItemRealPosition(int position) {
        return mWidget.getItemRealPosition(position);
    }

    @Override
    public int getFirstVisiblePosition() {
        return mWidget.getFirstVisiblePosition();
    }

    @Override
    public View getChildAt(int index) {
        return mWidget.getChildAt(index);
    }

    @Override
    public void onHeaderClick(View view) {
    }

    @Override
    public void onFooterClick(View view) {
    }

    @Override
    public void setSelection(int position) {
        mWidget.setSelection(position);
    }

    @Override
    public void smoothScrollToPosition(int position) {
        mWidget.smoothScrollToPosition(position);
    }

    @Override
    public void onDataSetChanged() {
    }

    @Override
    public int getHeaderViewPosition() {
        return mWidget.getHeaderViewPosition();
    }

    public WrapRecyclerView getRecyclerView() {
        return mWidget.getRv();
    }

    protected TreeRecyclerWidget<T, C> getWidget() {
        return mWidget;
    }

    @Override
    public void onAdapterClick(int position, View view) {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mWidget.onDestroy();
    }
}
